<HTML>
<HEAD>
<TITLE>[Chapter 2] 2.3 Dimension</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:24:51 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 2<br>Simple Graphics</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch02_04.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-2-SECT-3">2.3 Dimension</A></h2>

<P CLASS=para>
The <tt CLASS=literal>Dimension</tt> class is similar 
to the <tt CLASS=literal>Point</tt> class, except 
it encapsulates a width and height in a single object. Like <tt CLASS=literal>Point</tt>, 
<tt CLASS=literal>Dimension</tt> is somewhat underused; 
it is used primarily by methods that need to return a width and a height 
as a single object; for example, <tt CLASS=literal>getSize()</tt> 
returns a <tt CLASS=literal>Dimension</tt> object. 

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-2-SECT-3.1">Dimension Methods</A></h3>Variables

<P CLASS=para>
A <tt CLASS=literal>Dimension</tt> instance has two 
variables, one for width and one for height. They are accessible directly 
or through use of the <tt CLASS=literal>getSize()</tt> method. 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public int width </I><br>
<DD>

<P CLASS=para>
The width variable represents the size of an object along the x axis (left 
to right). Width should not be negative; however, there is nothing within 
the class to prevent this from happening. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public int height </I><br>
<DD>

<P CLASS=para>
The height variable represents the size of an object along the y axis (top 
to bottom). Height should not be negative; however, there is nothing within 
the class to prevent this from happening. </DL>
Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Dimension () </I><br>
<DD>

<P CLASS=para>
This constructor creates a <tt CLASS=literal>Dimension</tt> 
instance with a width and height of 0. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Dimension (Dimension dim) </I><br>
<DD>

<P CLASS=para>
This constructor creates a copy of <tt CLASS=literal>dim</tt>. The initial width is <tt CLASS=literal>dim.width</tt>. 
The initial height is <tt CLASS=literal>dim.height</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Dimension (int width, int height) </I><br>
<DD>

<P CLASS=para>
This constructor creates a <tt CLASS=literal>Dimension</tt> 
with an initial width of <tt CLASS=literal>width</tt> 
and an initial height of <tt CLASS=literal>height</tt>. </DL>
Sizing

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Dimension getSize () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getSize()</tt> method retrieves 
the current size as a new <tt CLASS=literal>Dimension</tt>, 
even though the instance variables are public. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void setSize (int width, int height) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setSize()</tt> method changes 
the dimension's size to <tt CLASS=literal>width</tt> 
x <tt CLASS=literal>height</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void setSize (Dimension d) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setSize()</tt> method changes 
the dimension's size to <tt CLASS=literal>d.width</tt> 
x <tt CLASS=literal>d.height</tt>. </DL>
Miscellaneous methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean equals (Object object) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>equals()</tt> method overrides the 
<tt CLASS=literal>Object.equals()</tt> 
method to define equality for dimensions. Two <tt CLASS=literal>Dimension</tt> 
objects are equal if their width and height values are equal. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public String toString () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>toString()</tt> method of <tt CLASS=literal>Dimension</tt> 
returns a string showing the current width and height settings. For example: 

<DIV CLASS=screen>
<P>
<PRE>
java.awt.Dimension[width=0,height=0]
</PRE>
</DIV>

</DL>
</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch02_04.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Point</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Shape</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
